package com.it.jzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.jzs.dao.BlogUserThumbsMapper;
import com.it.jzs.dao.admin.AdminBlogMapper;
import com.it.jzs.pojo.Blog;
import com.it.jzs.pojo.BlogUserThumbs;
import com.it.jzs.service.BlogUserThumbsService;
import com.it.jzs.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlogUserThumbsServiceImpl implements BlogUserThumbsService {

    @Resource
    private AdminBlogMapper blogMapper;
    @Resource
    private BlogUserThumbsMapper blogUserThumbsMapper;

    private static final Integer SELECT = 0;
    private static final Integer NOT_SELECT = 1;  // 不能选择的

    @Override  // 先根据用户id查出属于 博客，再通过博客查出中间表的数据
    public Result<Object> selectAll(Integer id) {
//        System.out.println("id = " + id);
        QueryWrapper<Blog> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id", id);
        List<Blog> blogs = blogMapper.selectList(wrapper1);

        ArrayList<Integer> list = new ArrayList<>();
        for (Blog blog : blogs) {  // 找出用户对应的博客id
            list.add(blog.getId());
        }

        QueryWrapper<BlogUserThumbs> wrapper = new QueryWrapper<>();
        wrapper.in("blog_id", list);
        wrapper.orderByDesc("create_time");

        List<BlogUserThumbs> blogUserThumbs = blogUserThumbsMapper.selectList(wrapper);
        for (BlogUserThumbs blogUserThumb : blogUserThumbs) { // 时间戳转换
            Long createTime = blogUserThumb.getCreateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(createTime);
            String format = simpleDateFormat.format(date);
            blogUserThumb.setFormatTime(format);
        }
        return Result.success(blogUserThumbs, "获取成功");
    }

    @Override
    public Result<Object> selectThumbsCount(Integer id) {  // 首页 收到的赞数量
        QueryWrapper<Blog> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id", id);
//        System.out.println("id = " + id);
        List<Blog> blogs = blogMapper.selectList(wrapper1);

        ArrayList<Integer> list = new ArrayList<>();
        for (Blog blog : blogs) {  // 找出用户对应的博客id
            list.add(blog.getId());
//            System.out.println("blog = " + blog);
        }
        if (list.size() != 0) {   // 非空判断，因为有些用户没有博客，所以数据是为空的，那么下面查询in就会传一个空数组是有问题的
            QueryWrapper<BlogUserThumbs> wrapper = new QueryWrapper<>();
            wrapper.eq("flag", SELECT);  // 查询没点过的

            wrapper.in("blog_id", list);
            Integer count = blogUserThumbsMapper.selectCount(wrapper);
            if (count > 0) {    // 大于0就在首页展示
                return Result.success(count, "查询未点击的点赞数成功");

            }

        }
        return Result.error(null, "查询失败");
//        List<BlogUserThumbs> blogUserThumbs = blogUserThumbsMapper.selectList(wrapper);

    }

    @Override
    public Result<Object> updateFlag(Integer id) { // 根据用户id查出对应的博客 修改点赞的状态
        QueryWrapper<Blog> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id", id);
        List<Blog> blogs = blogMapper.selectList(wrapper1);

        ArrayList<Integer> list = new ArrayList<>();
        for (Blog blog : blogs) {  // 找出用户对应的博客id
            list.add(blog.getId());
        }

        QueryWrapper<BlogUserThumbs> wrapper = new QueryWrapper<>();
        wrapper.in("blog_id", list);
        wrapper.eq("flag", SELECT);  // 查询没点过的  SELECT也就是0，就是可以查询的
        List<BlogUserThumbs> blogUserThumbs = blogUserThumbsMapper.selectList(wrapper); // 查询出所有博客点赞记录
        for (BlogUserThumbs blogUserThumb : blogUserThumbs) {
            blogUserThumb.setFlag(NOT_SELECT); // 查看过的了
            blogUserThumbsMapper.updateById(blogUserThumb);
        }
        return Result.success(null, "修改状态成功");
//        Integer count = blogUserThumbsMapper.selectCount(wrapper);

    }
}
